package com.chunuo.userservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chunuo.commonbase.exception.GuliException;
import com.chunuo.commonutils.JwtUtils;
import com.chunuo.commonutils.MD5;
import com.chunuo.commonutils.ResultCode;
import com.chunuo.userservice.entity.UcenterMember;
import com.chunuo.userservice.entity.vo.RegisterVo;
import com.chunuo.userservice.mapper.UcenterMemberMapper;
import com.chunuo.userservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.nio.charset.CoderResult;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-21
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 登录
     * @param member
     * @return
     */
    @Override
    public String login(UcenterMember member) {
        //获取用户账号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //校验账号密码不能为空
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new GuliException(ResultCode.ERROR.getCode(),"账号或密码错误，请重新登录！");
        }

        //获取手机号是否存在
        UcenterMember ucenterMember = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile", mobile));

        //不存在则登录失败
        if(ucenterMember==null){
            throw new GuliException(ResultCode.ERROR.getCode(),"输入的账号不存在！");
        }

        //手机号存在判断密码是否正确
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(ResultCode.ERROR.getCode(),"账号或密码错误，请重新登录！");
        }

        //账号是否被禁用
        if(ucenterMember.getIsDisabled()){
            throw new GuliException(ResultCode.ERROR.getCode(),"账号已被禁用！");
        }

        //最后登录成功，生成jwt字符串
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        //返回
        return jwtToken;
    }

    /**
     * 注册
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的用户信息
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验信息不能为空
        if(isEmpty(mobile)||isEmpty(nickname)||isEmpty(password)||isEmpty(code)){
            throw new GuliException(ResultCode.ERROR.getCode(),"输入的信息有误！");
        }

        //注册时会获取验证码，而验证码获取后会存在redis中5分钟，通过手机号获取
        String aliyunCode = (String) redisTemplate.opsForValue().get(mobile);
        if(!code.equals(aliyunCode)){
            throw new GuliException(ResultCode.ERROR.getCode(),"验证码输入不正确！");
        }

        //然后判断该手机号是否被注册过
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if(count!=null&&count>0){
            throw new GuliException(ResultCode.ERROR.getCode(),"该手机号已经注册过！");
        }

        //最后加密密码、用户不禁用以及默认头像
        UcenterMember member = new UcenterMember();
        BeanUtils.copyProperties(registerVo,member);
        member.setPassword(MD5.encrypt(member.getPassword()));
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2091711702,2468700162&fm=111&gp=0.jpg");
        baseMapper.insert(member);

    }

    public Boolean isEmpty(String val){
        return StringUtils.isEmpty(val);
    }

    //根据openid判断
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer getRegisterCount(String day) {
        Integer count = baseMapper.getRegisterCount(day);
        return count;
    }

}
